function login(){
	waitingDialog();
	var CookieHotel=getCookie("CookieHotel");
	if($('#hotel_id').val().length >= 15){
		var hotelIdL = $('#hotel_id').val();
		var hotelIdS = "0";
	}else{
		if (CookieHotel!=null && CookieHotel!=""){
			var hotelIdL = atob(CookieHotel);
			var hotelIdS = $('#hotel_id').val();
		}
		else{
			$( "#loginShort" ).dialog({
			modal: true,
			buttons: {
				Ok: function() {
					closeWaitingDialog();
				$( this ).dialog( "close" );
				}
			}
			});
			return;
		}
	}
	var email = $('#email').val();
	var pass = $('#pass').val();
	var data = { func: "login",
			hotelIdL: hotelIdL,
			hotelIdS: hotelIdS,
			email: email,
			pass: pass
			}
	simpleserviceWithError("loginService",data,loginSuccess,loginTimeout);
}

function setCookie(e,t,n){var r=new Date;r.setDate(r.getDate()+n);var i=escape(t)+(n==null?"":"; expires="+r.toUTCString());document.cookie=e+"="+i}
function getCookie(e){var t,n,r,i=document.cookie.split(";");for(t=0;t<i.length;t++){n=i[t].substr(0,i[t].indexOf("="));r=i[t].substr(i[t].indexOf("=")+1);n=n.replace(/^\s+|\s+$/g,"");if(n==e){return unescape(r)}}}

function checkCookie()
{
	var CookieHotel=getCookie("CookieHotel");
	if (CookieHotel!=null && CookieHotel!="")
	{}
	else 
	{
		var CookieHotel = $("#hotel_id").val();
		if (CookieHotel!=null && CookieHotel!="")
		{
			//alert("not have cookie");
			setCookie("CookieHotel",btoa(CookieHotel),365);
		}
	}
}

function loginError(){
	closeWaitingDialog();
	$( "#loginFail" ).dialog({
		modal: true,
		buttons: {
			Ok: function() {
			$( this ).dialog( "close" );
			}
		}
	});
}

function loginTimeout(data){
	closeWaitingDialog();
	$( "#loginTimeout" ).dialog({
		modal: true,
		buttons: {
		Ok: function() {
			$( this ).dialog( "close" );
		}
		}
	});
}

function loginSuccess(data){
	if(data=="")
		loginError();
	else{
		var array = JSON.parse(data);
		var objArray = new Array();
		serDate = array["servertime"];
		objArray = array["data"];
		if(objArray != null){
			var intime = array["intime"];
			var checksession = array["checksession"];
			setCookie("CookieHotel",btoa(array.Full),365);
				if(intime == true && checksession == false){
					document.location.href = "/multilogin.jsp";
				}else{
					setting(serDate,objArray);
				}
		}else{
			loginError();
		}

	}
}

function setting(serDate,objArray){
	var t = new Date();
	var compareTime = new Date(serDate).getTime()-t.getTime();
	var currencyDecimalPlaces =objArray.currencyDecimalPlaces;
	var dateTimeFormat = objArray.dateFormat.dateTimeFormat;
	var abreviation = objArray.defaultCurrency.abreviation;
	var symbol = objArray.defaultCurrency.symbol;
	var hotelId = objArray.hotelId;
 
	if(objArray.timeZoneFormatShow24Hours){
		var timeZoneFormatShow24Hours = false;
	}else{
		var timeZoneFormatShow24Hours = true;
	}
	
	if(currencyDecimalPlaces != null && currencyDecimalPlaces != ""){
		setCookie("currencyDecimalPlaces",currencyDecimalPlaces,365);
	}
	
	if(dateTimeFormat != null && dateTimeFormat != ""){
		setCookie("dateTimeFormat",dateTimeFormat,365);
	}
	
	if(abreviation !=null && abreviation!= ""){
		setCookie("abreviation",abreviation,365);
	}
	
	if(symbol != null && symbol != ""){
		setCookie("symbol",symbol,365);
	}
	
	if(hotelId !=null && hotelId != ""){
		setCookie("hotelId",hotelId,365);
	}
	if(timeZoneFormatShow24Hours != null && timeZoneFormatShow24Hours != ""){
		setCookie("timeZoneFormatShow24Hours",timeZoneFormatShow24Hours,365);
	}
	if(compareTime !=null && compareTime != ""){
		setCookie("CompareTime",compareTime,365);
	}
	checkCookie();
	window.location.href="choseworkarea.jsp";
	
}

function continuelogin(){
	var url = "/loginService";
	var data = { func : "continuelogin"}
	simpleservice(url,data,loginSuccess);
}

function backtologin(){
	 window.location.href="index.jsp";
}